require "class"
require "ChatRecord"
ChatManager = class("ChatManager")

function ChatManager:ctor()
    self.pool = {}
end

local function GetKey(useridA, useridB)
    local res = 0
    local maxNum = 100000
    if useridA < useridB then
        res = useridA * maxNum + useridB
    else
        res = useridB * maxNum + useridA
    end
    return res
end

function ChatManager:AddRecord(userSend, userRec, content)
    -- 根据用户名确定查询key
    local key = GetKey(userRec.id, userSend.id)

    -- 构造记录对象
    local record = ChatRecord.new(userSend.username, userRec.username, content)
    record = record:simple()

    -- 如果还没有记录
    if self.pool[key] == nil then
        self.pool[key] = {record}
    else
        -- 如果已经有记录
        local records = self.pool[key]
        self.pool[key][#records + 1] = record
    end
end

function ChatManager:GetRecods(userSend, userRec)
    -- 根据用户名确定查询key
    local key = GetKey(userRec.id, userSend.id)

    if self.pool[key] == nil then
        return {}
    else
        return self.pool[key]
    end
end
